/*
 * NewOntologyPhotoCreator.java
 *
 * Created on 15 f�vrier 2008, 14:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.great.functionalCore;

import br.great.beans.*;
import java.io.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import org.w3c.dom.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geonames.WikipediaArticle;
//import java.sql.Timestamp;
import br.great.config.GetCheminServer;

/**
 *
 * @author defautce
 */
public class NewOntologyPhotoCreator {
    // create a new file containing the annotations (captured and derivated) of a photo
//    private static String serverfileAddress="D:\\Documents and Settings\\Administrator.3DF5CEE6F12E44B\\My Documents\\NetBeansProjects\\PhotoMapServer\\PhotoMapBeta_Web\\web\\";
    
    private static FileOutputStream newOnto;
    
    /** Creates a new instance of NewOntologyPhotoCreator */
    public NewOntologyPhotoCreator() {
    }
    
    public File write(String collectionName, Photo p, int i, String loginUser) {
        String pathPhotoOnto = GetCheminServer.serverfileAddress+"photos\\"+loginUser+"\\"+collectionName+"\\"+p.getUniqID()+".owl";
        try {
//            System.out.println("d�but write photo");
            File file = new File(pathPhotoOnto);
            if(!file.exists())
                file.createNewFile();
            newOnto = new FileOutputStream(file);

            // en-t�te xml
            String s1 = "<?xml version=\"1.0\"?>\n";

            
            s1 += "<rdf:RDF xmlns:gml=\"http://www.opengis.net/gml/\" \nxmlns:georss=\"http://www.georss.org/georss/\"\nxmlns:ctxt=\"http://www-lsr.imag.fr/users/Windson.Viana/ontologies/ContextTopOntology.owl#\"\nxmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\nxmlns:owl=\"http://www.w3.org/2002/07/owl#\"\nxmlns:ctxtphoto=\"http://www-lsr.imag.fr/users/Windson.Viana/ontologies/ContextPhoto.owl#\"\nxmlns:protege=\"http://protege.stanford.edu/plugins/owl/protege#\"\nxmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\nxmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\nxmlns:nexifdata=\"http://www.semanticdesktop.org/ontologies/2007/05/10/nexif/nexif_data.rdfs#\"\nxmlns:dc=\"http://purl.org/dc/elements/1.1/\"\nxmlns:nexif=\"http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#\"\nxmlns:time=\"http://www.w3.org/2006/time#\"\n";
            if (p.getURIAnnotations() != null) {
                s1 += "xmlns=\"" + p.getURIAnnotations() + "#\"\n";
                s1 += "xml:base=\"" + p.getURIAnnotations() + "\">\n";
            }
//            System.out.println(1);

            
            s1 += "<owl:Ontology rdf:about=\"\">\n";
            s1 += "<owl:imports rdf:resource=\"http://www-lsr.imag.fr/users/Windson.Viana/ontologies/ContextPhoto.owl\"/>\n";
            s1 += "</owl:Ontology>\n";
//            System.out.println(2);

            // photo_shot instant
            String dateB = "";
            if (p.getDateTime() != null) {
                s1 += "<ctxtphoto:Photo_Shot_Instant rdf:ID=\"photo" + i + "Instant\"> \n";
                Date b = p.getDateTime();
                String month = (b.getMonth() + 1) + "";
                if (month.length() < 2) {
                    String month2 = "0" + month;
                    month = month2;
                }
                String day = b.getDate() + "";
                if (day.length() < 2) {
                    String day2 = "0" + day;
                    day = day2;
                }
                String hours = b.getHours() + "";
                if (hours.length() < 2) {
                    String hours2 = "0" + hours;
                    hours = hours2;
                }
                String minutes = b.getMinutes() + "";
                if (minutes.length() < 2) {
                    String minutes2 = "0" + minutes;
                    minutes = minutes2;
                }
                String seconds = b.getHours() + "";
                if (seconds.length() < 2) {
                    String seconds2 = "0" + seconds;
                    seconds = seconds2;
                }
                dateB = b.getYear() + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
                s1 += "<time:inXSDDateTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" + dateB + "</time:inXSDDateTime>\n";
                s1 += " <time:inDateTime> \n<time:DateTimeDescription rdf:ID=\"DateTimeDescriptionPhoto\">\n";
                s1 += "<time:hours rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + hours + "</time:hours> \n<time:years rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + b.getYear() + "</time:years> \n<time:months rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + month + "</time:months>\n<time:days rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + day + "</time:days> \n <time:minutes rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + minutes + "</time:minutes> \n<time:seconds rdf:datatype=\"http://www.w3.org/2001/XMLSchema#decimal\">" + seconds + "</time:seconds> \n";
                s1 += "</time:DateTimeDescription> \n</time:inDateTime>";
                s1 += "  </ctxtphoto:Photo_Shot_Instant>";
            }
//            System.out.println(3);
            

            // wikipedia object
            if (p.getNearByWikiObject() != null) {
                Iterator itW = p.getNearByWikiObject().iterator();
                int numW = 1;
                while (itW.hasNext()) {
                    WikipediaArticle art = (WikipediaArticle) itW.next();
                    s1 += "<ctxtphoto:WikipediaEntry rdf:ID=\"WikipediaEntry" + numW + "\">\n";
                    s1 += "<ctxtphoto:url xml:lang=\"en\">" + art.getWikipediaUrl() + "</ctxtphoto:url>\n";
                    s1 += "<ctxtphoto:title xml:lang=\"en\">" + art.getTitle() + "</ctxtphoto:title>\n";
                    s1 += "<ctxtphoto:description xml:lang=\"en\">" + art.getSummary() + "</ctxtphoto:description>\n";
                    s1 += "<georss:where> <gml:Point rdf:ID=\"Point_WikipediaEntry\"" + numW + "\">\n";
                    s1 += "<gml:pos rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + art.getLatitude() + " " + art.getLongitude() + "</gml:pos>\n";
                    s1 += "</gml:Point> \n</georss:where> \n</ctxtphoto:WikipediaEntry>\n";
                    numW++;
                }
            }
//            System.out.println(4);

            
            s1 += "<ctxtphoto:Place rdf:ID=\"place\">\n";
            s1 += "<georss:where> \n<gml:Point rdf:ID=\"point\">\n";
            if (p.getCoor() != null) {
                s1 += "<gml:pos rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getCoor().getLatitude() + " " + p.getCoor().getLongitude() + "</gml:pos> \n</gml:Point> \n</georss:where>";
            }
            if (p.getAddress() != null) {
                s1 += "<ctxtphoto:address> \n<ctxtphoto:Address rdf:ID=\"AddressPhoto\">\n";
                String address = "";
                if (p.getAddress().getNameRoad() != null) {
                    s1 += "<ctxtphoto:street_address rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getAddress().getNameRoad() + "</ctxtphoto:street_address>\n";
                    address += p.getAddress().getNameRoad() + ", ";
                }
                if (p.getAddress().getNameCity() != null) {
                    s1 += "<ctxtphoto:city rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getAddress().getNameCity() + "</ctxtphoto:city>\n";
                    address += p.getAddress().getNameCity() + ", ";
                }
                if (p.getAddress().getNameCountry() != null) {
                    s1 += "<ctxtphoto:country rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getAddress().getNameCountry() + "</ctxtphoto:country>\n";
                    address += p.getAddress().getNameCountry();
                }
                s1 += "</ctxtphoto:Address>\n</ctxtphoto:address>\n";
                s1 += "<ctxtphoto:textual_description xml:lang=\"en\">" + address + "</ctxtphoto:textual_description>\n"; // = address
            }
            s1 += "</ctxtphoto:Place>\n";
//            System.out.println(5);

            
            if (p.getContentAnnotation() != null) {
                s1 += "<ctxtphoto:Textual_Description rdf:ID=\"Textual_Description_Photo" + i + "\">\n";
                s1 += "<ctxtphoto:textual_description xml:lang=\"en\">" + p.getContentAnnotation() + "</ctxtphoto:textual_description>\n";
                s1 += "</ctxtphoto:Textual_Description>\n";
            }
//            System.out.println(6);

            // tags
            Vector<Tag> tags = p.getTags();
            if (tags != null) {
                Iterator itTags = tags.iterator();
                int nTag = 1;
                while (itTags.hasNext()) {
                    Tag tag = (Tag) itTags.next();
                    s1 += "<ctxtphoto:content_annotation> \n<ctxtphoto:Tag rdf:ID=\"tag" + nTag + "\"> \n<ctxtphoto:tag rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + tag.getTAG_VALUE() + "</ctxtphoto:tag> \n</ctxtphoto:Tag> \n</ctxtphoto:content_annotation>\n";
                    nTag++;
                }
            }
//            System.out.println(7);

            
            Vector<String> dev = p.getDevices();
            if (dev != null) {
                Iterator itD = dev.iterator();
                int numD = 1;
                while (itD.hasNext()) {
                    String d = (String) itD.next();
                    s1 += "<ctxtphoto:BTDevice rdf:ID=\"BTDevice_" + numD + "\"> \n<ctxtphoto:bluetoothAddress rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + d + "</ctxtphoto:bluetoothAddress>\n</ctxtphoto:BTDevice>\n";
                    numD++;
                }
            }
//            System.out.println(8);

            
            if (p.getWeather() != null | p.getSeason() != null) {
                s1 += "<ctxtphoto:Physical_Environment rdf:ID=\"Physical_Environment_Photo" + i + "\">\n";
                if (p.getWeather() != null) {
                    if (p.getTemperature() != null) {
                        if (p.getTemperature().getType() != null) {
                            s1 += "<ctxtphoto:temperature_unit rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getTemperature().getType() + "</ctxtphoto:temperature_unit>\n";
                            s1 += "<ctxtphoto:temperature_value rdf:datatype=\"http://www.w3.org/2001/XMLSchema#float\">" + p.getTemperature().getValue() + "</ctxtphoto:temperature_value>\n";
                        }
                    }
                }
                if (p.getSeason() != null) {
                    s1 += "<ctxtphoto:season rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getWeather().getDescription() + "</ctxtphoto:season>\n";
                }
                s1 += "<georss:where rdf:resource=\"#point\"/>\n";
                s1 += "</ctxtphoto:Physical_Environment>\n";
            }
//            System.out.println(9);
            
//            String s2 = "";
            // shot context
            s1 += "<ctxtphoto:Photo_Shot rdf:ID=\"photoShot\"> \n<ctxtphoto:produces> \n<ctxtphoto:Photo rdf:ID=\"photo" + i + "\">\n";
            if (p.getUniqID() != null) {
                s1 += "<nexif:imageUniqueID xml:lang=\"en\">" + p.getUniqID() + "</nexif:imageUniqueID>\n";
            }
            s1 += "<dc:format rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">jpeg</dc:format> \n<dc:type rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">image</dc:type>\n";
            if (p.getCreator() != null) {
                s1 += "<dc:creator rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getCreator() + "</dc:creator>\n";
            }
            if (p.getName() != null) {
                s1 += "<dc:title rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">" + p.getName() + "</dc:title>\n";
            }
            //s1 += <nexif:height rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">"+...+"</nexif:height>";
            if (p.getURIPhoto() != null) {
                s1 += "<nexif:fileSource xml:lang=\"en\">" + p.getURIPhoto() + "</nexif:fileSource>";
            }
            s1 += "<nexif:datatype xml:lang=\"en\">jpeg</nexif:datatype>";
            if (p.getContentAnnotation() != null) {
                s1 += "<nexif:userComment xml:lang=\"en\">" + p.getContentAnnotation() + "</nexif:userComment> _n";
            }
            s1 += "<ctxtphoto:content_annotation rdf:resource=\"#Textual_Description_Photo" + i + "/>\n";
            //s1 += "<nexif:imageWidth xml:lang=\"en\">"+...+"</nexif:imageWidth>";
            if (p.getModel() != null) {
                s1 += "<nexif:model xml:lang=\"en\">" + p.getModel() + "</nexif:model>\n";
            }
            if (p.getDateTime() != null) {
                s1 += "<nexif:dateTime rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">" + dateB + "</nexif:dateTime>\n";
            }
//            System.out.println(10);

            s1 += "<ctxtphoto:shot_context> \n<ctxt:Context rdf:ID=\"context_photo" + i + "\">\n";
           // System.out.println(11);

            if (p.getDateTime() != null) {
                s1 += "<ctxt:hasContextElement rdf:resource=\"#photo" + i + "Instant\"/>\n";
            }

            s1 += "<ctxt:hasContextElement rdf:resource=\"#place\"/>\n";
//            System.out.println(12);
//            System.out.println("TAILLE : "+s1.length());

            Vector<String> dev2 = p.getDevices();
//            System.out.println("size dev2 : "+dev2.size());
            if (dev2 != null) {
                Iterator itD2 = dev2.iterator();
                int numD2 = 1;
                while (itD2.hasNext()) {
                    itD2.next();
                    s1 += "<ctxt:hasContextElement rdf:resource=\"#BTDevice_" + numD2 + "\"/>\n";
//                    System.out.println("numDev : "+numD2);
                    numD2++;
                }
            }
//            System.out.println("TAILLE : "+s1.length());
//            System.out.println(13);

            Vector<Wiki> wiki2 = p.getNearByWikiObject();
            if (wiki2 != null) {
                Iterator itW2 = wiki2.iterator();
                int numW2 = 1;
                while (itW2.hasNext()) {
                    itW2.next();
                    s1 += "<ctxt:hasContextElement rdf:resource=\"#WikipediaEntry" + numW2 + "\"/>\n";
                    numW2++;
                }
            }
//            System.out.println(14);

            if (p.getWeather() != null | p.getSeason() != null) {
                s1 += "<ctxt:hasContextElement rdf:resource=\"#Physical_Environment_Photo" + i + "\"/>\n";
            }
           // System.out.println(15);

            s1 += "<ctxt:describesTheSituationOf rdf:resource=\"#photoShot\"/> \n</ctxt:Context> \n</ctxtphoto:shot_context>\n";

            s1 += "</ctxtphoto:Photo>\n</ctxtphoto:produces>\n";

            s1 += "<ctxt:hasContext rdf:resource=\"#context_photo" + i + "\"/>\n";

            s1 += "</ctxtphoto:Photo_Shot>\n</rdf:RDF>";
            
//            System.out.println(16);
//            System.out.println("FIN NewOntologyPhotoCreator");
//            System.out.println(s1);
            byte[] annot = s1.getBytes();
            newOnto.write(annot);
            return file;
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(NewOntologyPhotoCreator.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }   
    
    
    public static void main (String[] args){
        try {
            //System.out.println(removeDossier("P:\\Bureau\\projet_PHOTOMAP\\CONTEXT_PHOTO\\fichiersOwlMobile\\romanForum1.owl"));
            NewOntologyPhotoCreator o = new NewOntologyPhotoCreator();
            Photo p = new Photo();
            p.setName("grenoble1");
            p.setUniqID("grenoble");
            p.setURIPhoto("steamer.grenoble1");
            p.setContentAnnotation("textual description...");
            Tag t1 = new Tag();
            t1.setTAG_VALUE("tag1");
            Tag t2 = new Tag();
            t2.setTAG_VALUE("tag2");
            Vector<Tag> t = new Vector<Tag>();
            t.add(t1);
            t.add(t2);
            p.setTags(t);
            p.setCreator("celine");
            Vector<String> dev = new Vector<String>();
            dev.add("5164654");
            dev.add("16684ZS6841");
            p.setDevices(dev);
            Point point = new Point();
            point.setLatitude(12.3);
            point.setLongitude(23.4);
            p.setCoor(point);
            p.setURIAnnotations("Steamer.Annotation.Grenoble1");
            Temperature temperature = new Temperature();
            p.setTemperature(temperature);
            Weather weather = new Weather();
            weather.setDescription("description Weather");
            Address address = new Address();
            address.setNameCity("Paris");
            address.setNameCountry("France");
            address.setNameRoad("Rue1");
            p.setWeather(weather);
            p.setDateTime(Timestamp.valueOf("2008-02-05 10:22:34"));
            p.setPartOfDay("morning");
            p.setANNOTATION_VALIDATED(false);
            p.setSeason("Spring");
            File f = o.write("stmalo", p, 1, "windson");
            System.out.println("PATH : "+f.getAbsolutePath());
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
}
